package ru.poomsae.isap.Online.Para

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.example.isap_wtf.Model.ApiEndPoint
import com.example.isap_wtf.Model.ParametrsISAP
import ru.poomsae.isap.Model.paraJSON
import ru.poomsae.isap.Online.SystemCut.PresentationActivity
import ru.poomsae.isap.R

class ParaAcc  : AppCompatActivity() {

    var armsT = 20
    var legsT = 20
    var restartT = 0
    var penalT = 0
    var bonusT = 0
    var gmT = 0
    var accyInt = 40
    var accy: Double = 4.0
    var onlineConSys = ParametrsISAP.onlineConSys
    var mIDHost: String = ParametrsISAP.idHostServer
    var url = ""
    var idvistuplenie = "null"
    lateinit var myTextView: TextView
    lateinit var myLinearLayout: LinearLayout
    lateinit var myRelativeLayout: LinearLayout
    lateinit var mySudTextView: TextView
    lateinit var myPoomsaeTextView: TextView
    lateinit var myzaprosJSON: paraJSON


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.para)
        url = "http://" + mIDHost + ApiEndPoint.VISTUPLENIE

        myTextView = findViewById(R.id.idvistTextPara) as TextView
        myLinearLayout = findViewById(R.id.progressBarPara) as LinearLayout
        myRelativeLayout = findViewById(R.id.para) as LinearLayout
        mySudTextView = findViewById(R.id.sudNumTV) as TextView

        myzaprosJSON = paraJSON(myTextView, myLinearLayout, myRelativeLayout, mySudTextView)
        val decorView = window.decorView
        val uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN

        if(onlineConSys == 1) {
            myzaprosJSON.waitVistup()
            visibleSystem()
            decorView.systemUiVisibility = uiOptions
            getSupportActionBar()?.hide()
        }

        if (onlineConSys == 0) {
            mySudTextView.visibility = View.GONE
            myPoomsaeTextView.visibility = View.GONE
        }

        val arms = findViewById<RadioGroup>(R.id.arms)
        val armsText = findViewById<TextView>(R.id.armsText)
        val accyp = findViewById<TextView>(R.id.accyp)
        arms.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                armsT = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                armsText.setText(radio.text as String)
                accy = (armsT + legsT - restartT + bonusT - penalT - gmT).toDouble()/(10).toDouble()
                accyp.setText(accy.toString())
            })

        val legs = findViewById<RadioGroup>(R.id.legs)
        val legsText = findViewById<TextView>(R.id.legsText)
        legs.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                legsT = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                legsText.setText(radio.text as String)
                accy = (armsT + legsT - restartT + bonusT - penalT - gmT).toDouble()/(10).toDouble()
                accyp.setText(accy.toString())
            })

        val res = findViewById<RadioGroup>(R.id.res)
        val restartText = findViewById<TextView>(R.id.restartText)
        res.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                restartT = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                restartText.setText(radio.text as String)
                accy = (armsT + legsT - restartT + bonusT - penalT - gmT).toDouble()/(10).toDouble()
                accyp.setText(accy.toString())
            })

        val bonus = findViewById<RadioGroup>(R.id.bonus)
        val bonusText = findViewById<TextView>(R.id.bonusText)
        bonus.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                bonusT = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                bonusText.setText(radio.text as String)
                accy = (armsT + legsT - restartT + bonusT - penalT - gmT).toDouble()/(10).toDouble()
                accyp.setText(accy.toString())
            })

        val gmR = findViewById<RadioGroup>(R.id.gmR)
        val gmText = findViewById<TextView>(R.id.gmText)
        gmR.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                gmT = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                gmText.setText(radio.text as String)
                accy = (armsT + legsT - restartT + bonusT - penalT - gmT).toDouble()/(10).toDouble()
                accyp.setText(accy.toString())
            })

        val penal = findViewById<RadioGroup>(R.id.penal)
        val penalText = findViewById<TextView>(R.id.penalText)
        penal.setOnCheckedChangeListener(
            RadioGroup.OnCheckedChangeListener { group, checkedId ->
                val radio: RadioButton = findViewById(checkedId)
                penalT = ((radio.text as String).toDouble() * (10).toDouble()).toInt()
                penalText.setText(radio.text as String)
                accy = (armsT + legsT - restartT + bonusT - penalT - gmT).toDouble()/(10).toDouble()
                accyp.setText(accy.toString())
            })

        val btnPres = findViewById<Button>(R.id.btnPres)
        btnPres.setOnClickListener {
            val intent = Intent (this, PresentationActivity::class.java)
            intent.putExtra("accuracyDouble", accy)
            startActivity(intent)
        }
    }

    @SuppressLint("WrongViewCast")
    fun visibleSystem() {

        if (idvistuplenie == "null") {
            val para = findViewById<LinearLayout>(R.id.para)
            val progressBarPara = findViewById<ProgressBar>(R.id.progressBarFree)
            val idvistTextPara = findViewById<TextView>(R.id.idvistTextPara)
            para.visibility = View.GONE
            progressBarPara.visibility = View.VISIBLE
            idvistTextPara.setText(getString(R.string.idvistTextView))
        }
    }

}